<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小西瓜 <751707632@qq.com.com>
// +----------------------------------------------------------------------
namespace app\api\model;

use app\admin\model\RouteModel;
use think\Model;
use think\Db;
use app\fowl\model\ImsMeetingJoinModel;

class ImsMeetingLiveModel extends Model
{
    /**
     * 会议数据添加
     * @params array $data  存储数据
     * @return array
     */
    public function set($data)
    {
    	if ( empty($data) ) {
            return ['code'=>'0', 'msg'=>'缺少参数'];
        }
        $data_new = array();
        if(isset($data['user_id']))         $data_new['user_id']        = $data['user_id'];
        if(isset($data['vm_title']))        $data_new['vm_title']       = $data['vm_title'];
        if(isset($data['type_id']))         $data_new['type_id']        = $data['type_id'];
        if(isset($data['join_way']))        $data_new['join_way']       = $data['join_way'];
        if(isset($data['compere_id']))      $data_new['compere_id']     = $data['compere_id'];
        if(isset($data['compere_name']))    $data_new['compere_name']   = $data['compere_name'];
        if(isset($data['start_time']))      $data_new['start_time']     = $data['start_time'];
        if(isset($data['duration']))        $data_new['duration']       = $data['duration'];
        if(isset($data['cover_pic']))       $data_new['cover_pic']      = $data['cover_pic'];
        if(isset($data['intro']))           $data_new['intro']          = $data['intro'];
        if(isset($data['join_address_id'])) $data_new['join_address_id']    = $data['join_address_id'];
        if(isset($data['status']))          $data_new['status']         = $data['status'];
        if(isset($data['is_vm']))           $data_new['is_vm']          = $data['is_vm'];

        // 判断是增加还是修改
        if(!empty($data['id']) && ($id = intval(trim($data['id'])))) {
            $data_new['update_time'] = date('Y-m-d H:i:s', time());
            $this->where('id', $id)->update($data_new);
            return ['code'=>'1', 'msg'=>'修改成功'];
        } else {
            $data_new['create_time'] = date('Y-m-d H:i:s', time());
            $id = $this->insertGetId($data_new);
            if ($id && isset($data['join'])) {
                foreach ( $data['join'] as $v ) {
                    $join['vm_id'] = $id;
                    $join['is_vm'] = 2;
                    if(isset($v['join_id']))   $join['join_id'] = $v['join_id'];
                    if(isset($v['is_must']))   $join['is_must'] = $v['is_must'];
                    Db::name('ims_meeting_join')->insert($join);
                }
            } 
            return ['code'=>'1', 'msg'=>'添加成功'];
        }
    }

    /**
     * 会议详情-未开始
     * @param [array] $[where] [<查询条件>]
     * @return   [<array|string>]
     */
    public function meetingDateilsFuture($where=array())
    {
        $info = $this->alias('vm')
        ->join('__IMS_MEMBER__ im','vm.user_id = im.id', 'left')
        ->join('__IMS_ADDRESS__ area','vm.join_address_id = area.id', 'left')
        ->field('vm.id,vm.vm_title,vm.cover_pic,vm.intro,vm.compere_id,vm.compere_name,vm.create_time,im.staff_name,area.province_id,area.city_id,area.area_id,area.address')
        ->where($where)->find()->toArray();

        $info['cover_pic'] = cmf_get_image_url(str_replace("\\","/",$info['cover_pic'])); // 处理图片
        $info['compere_intro'] = Db::name('ims_member')->where('id', $info['compere_id'])->value('staff_intro'); // 主持人简介
        $info['province'] = Db::name('ims_regions')->where('region_id',$info['province_id'])->value('region_name');
        $info['city'] = Db::name('ims_regions')->where('region_id',$info['city_id'])->value('region_name');
        $info['area'] = Db::name('ims_regions')->where('region_id',$info['area_id'])->value('region_name');

        // 获取参会人员列表
        $confereeModel = new ImsMeetingJoinModel();
        $info['confereeList'] = $confereeModel->confereeList(['vm_id'=>$info['id']]);

        return $info;
    }

    /**
     * 会议回放
     * @param   [array] $[where] [<查询条件>]
     * @return   [<array>]
     */
    public function meetingPlayback($where=array())
    {
        $info = $this->alias('vm')
        ->join('__IMS_MEMBER__ im','vm.user_id = im.id', 'left')
        ->field('vm.id,vm.vm_title,vm.cover_pic,vm.intro,vm.store_link,vm.compere_id,vm.compere_name,vm.create_time')
        ->where($where)->find()->toArray();

        $info['cover_pic'] = cmf_get_image_url(str_replace("\\","/",$info['cover_pic'])); // 处理图片
        $info['compere_intro'] = Db::name('ims_member')->where('id', $info['compere_id'])->value('staff_intro'); // 主持人简介

        return $info;
    }

    /**
     * 会议列表
     * @param  [array] $[where] [<查询条件>]
     * @param  [int] $[page] [<分页>]
     * @param  [int] $[limit] [<显示数量>]
     * @param  [string] $[order] [<排序规则>]
     * @return   [<array>]
     */
    public function meetingList($where=array(), $page=1, $limit=5, $order='vm.id desc')
    {
        $list = $this->alias('vm')
        ->join('__IMS_MEMBER__ im','im.id = vm.compere_id', 'left')
        ->field('vm.id,vm.vm_title,vm.cover_pic,vm.start_time,im.staff_duty,im.staff_deptname')
        ->where($where)->order($order)->page($page, $limit)->select()->toArray();
        foreach ( $list as $key => $val ) {
            $list[$key]['cover_pic'] = cmf_get_image_url(str_replace("\\","/",$val['cover_pic'])); // 处理图片
        }

        $count = $this->alias('vm')
        ->join('__IMS_MEMBER__ im','im.id = vm.compere_id', 'left')
        ->where($where)->count();

        return ['list'=>$list, 'count'=>$count];
    } 


    /**
     * 根据ID查询我的会议列表
     * @param arry $where 查询条件
     * @param [int] $[page] [<分页>]
     * @param [int] $[limit] [<显示条数>]
     * @param string $order 查询顺序
     * @return array
     */
    public function myMeetingList($where=array(), $page=1, $limit=5, $order='vm.id desc, vm.status asc')
    {
        $list = $this->alias('vm')
        ->join('__IMS_MEMBER__ im','im.id = vm.user_id', 'left')
        ->join('__PORTAL_CATEGORY__ pc','vm.type_id = pc.id', 'left')
        ->field('pc.name,vm.vm_title,vm.compere_id,vm.cover_pic,vm.compere_name,vm.start_time,vm.duration,vm.status,im.staff_name,im.staff_duty,im.staff_deptname')
        ->where($where)->order($order)->page($page, $limit)->select()->toArray();
        foreach ( $list as $key => $val ) {
            $list[$key]['cover_pic'] = cmf_get_image_url(str_replace("\\","/",$val['cover_pic'])); // 处理图片
            $list[$key]['status_name'] = meetingStatus()[$val['status']];
            $list[$key]['ctegory_name'] = $val['name'];
            unset($list[$key]['name']);

            // // 查询参会记录
            // $joinLogModel = new  ImsJoinLogModel();
            // $joinInfo = $joinLogModel->joinLogShow(['vm_id'=>$val['id'], 'is_vm'=>2], $val['start_time'], $val['duration']);
            // $list[$key]['join_info'] = $joinInfo;
            // $list[$key]['join_status'] = $joinInfo['join_status'];
        }

        $count = $this->alias('vm')
        ->join('__IMS_MEMBER__ im','im.id = vm.compere_id', 'left')
        ->join('__PORTAL_CATEGORY__ pc','vm.type_id = pc.id', 'left')
        ->where($where)->count();

        return ['list'=>$list, 'count'=>$count];
    }
}